<?php

class RentalController extends BaseController
{
    protected $tableName = "rental";
    protected $pk = "rent_id";

    // 重写获取方法
    public function get($id): array
    {
        $sql = "SELECT r.rent_id, c.customer_id, c.last_name, c.first_name,
                    b.bag_id, b.bag_name,
                    r.date_rented, r.date_returned,r.has_insurance, mnf.*
                FROM bag b, customer c, rental r, manufacturer mnf
                WHERE r.customer_id = c.customer_id
                        AND r.bag_id = b.bag_id
                        AND b.mnf_id = mnf.mnf_id
                    AND r.rent_id = $id;";
        $result = $this->conn->query($sql);
        $data = $this->getSingleDataFromResult($result);

        if (empty($data)) {
            return ReturnHelper::failed([], "rentalId不存在", 404);
        }
        else {
            if ($data['date_returned']===null) {
                // 如果包还没有归还返回这个
                return ReturnHelper::success($data, "数据请求成功");
            }
            else {
                // 如果包已归还返回统计数据
                $sql = "SELECT r.rent_id, c.customer_id, c.last_name, c.first_name,  
                        b.bag_id, b.bag_name,
                        r.date_rented, r.date_returned,r.has_insurance, mnf.*,
                        DATEDIFF(r.date_returned,r.date_rented) `during`,
                        DATEDIFF(r.date_returned,r.date_rented) * IF(r.has_insurance, 1 + mnf.price_per_day ,mnf.price_per_day) `cost`
                FROM customer c, rental r, bag b, manufacturer mnf
                WHERE r.customer_id = c.customer_id
                        AND r.bag_id = b.bag_id
                        AND b.mnf_id = mnf.mnf_id
                    AND r.rent_id = $id;";
                $result = $this->conn->query($sql);
                $data = $this->getSingleDataFromResult($result);
                return ReturnHelper::success($data, "数据请求成功");
            }
        }
    }

    // 重写getList方法
    public function getList(): array
    {
        // 暂时采用统一不返回during和cost的形式
        $sql = "SELECT r.rent_id, c.customer_id, c.last_name, c.first_name,
                    b.bag_id, b.bag_name,
                    r.date_rented, r.date_returned,r.has_insurance,
                    mnf.*
                FROM bag b, customer c, rental r, manufacturer mnf
                WHERE r.customer_id = c.customer_id
                        AND r.bag_id = b.bag_id
                        AND b.mnf_id = mnf.mnf_id
                ORDER BY r.rent_id";
        $result = $this->conn->query($sql);
        $data = $this->getArrayDataFromResult($result);

        return ReturnHelper::success($data, "rental列表数据请求成功");
    }

    // 重写新增方法
    public function post($obj): array
    {
        $customer_id = $obj['customer_id'];
        $bag_id = $obj['bag_id'];
        $date_rented = $obj['date_rented'];
        $has_insurance = $obj['has_insurance'];
        $has_insurance = ($has_insurance===true) ? 1 : 0;
        $sql = "CALL add_rental(\"$customer_id\", '$date_rented', $bag_id, $has_insurance);";
        $result = $this->conn->query($sql);
        $flag = $this->judge($result);
        if ($flag) {
            return ReturnHelper::success([], "新增rental记录成功");
        }
        else {
            return ReturnHelper::failed([], "新增rental记录失败");
        }
    }

    // PUT
    public function rentReturn($rentalId, $obj): array {
        $date_returned = $obj['date_returned'];
        $sql = "UPDATE rental SET date_returned=\"$date_returned\" WHERE rent_id = $rentalId";
        $result = $this->conn->query($sql);
        $flag = $this->judge($result);
        if ($flag) {
            $ret = $this->get($rentalId);
            return ReturnHelper::success($ret['data'], "rental：$rentalId 归还成功");
        }
        else {
            return ReturnHelper::failed([], "rental：$rentalId 归还失败");
        }
    }
}